﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using SMS;

namespace SMS.DAL.Providers
{
    public class UserProvider : ProviderBase<User, UserSearchCriteria>
    {

        public override void Insert(User item)
        {
            Context.Users.AddObject(item);
            Context.SaveChanges();
        }

        public override void Update(User updatedItem)
        {
            var u = this.GetByID(updatedItem.ID);
            u.Address = updatedItem.Address;
            u.Email = updatedItem.Email;
            u.Mobile = updatedItem.Mobile;
            u.Name = updatedItem.Name;
            u.Password = updatedItem.Password;
            u.Phone = updatedItem.Phone;
            u.RoleID = updatedItem.RoleID;
            u.UserName = updatedItem.UserName;
            u.Status = updatedItem.Status;
            u.CurrentStock = updatedItem.CurrentStock;
            Context.SaveChanges();
        }

        public override void Delete(int id)
        {
            var deleted = this.GetByID(id);
            deleted.Status = (int)UserStatusEnum.Deleted;
            this.Update(deleted);
        }

        public override User GetByID(int id)
        {
            return (from c in Context.Users
                    where c.ID == id
                    select c).SingleOrDefault();
        }

        public override List<User> Search(UserSearchCriteria SearchCriteria)
        {
            return (from c in Context.Users.Include("Role")
                    where (SearchCriteria.UserName == null || c.Name.Contains(SearchCriteria.UserName)) &&
                          (SearchCriteria.RoleID == null || c.RoleID == SearchCriteria.RoleID) &&
                          (c.Status == (int)UserStatusEnum.Available) &&
                          (SearchCriteria.StockID == null || c.CurrentStock == SearchCriteria.StockID)
                    select c).ToList();
        }

        public User GetByUsernameAndPassword(string _Username, string _Password)
        {
            return (from u in Context.Users
                    where (u.UserName == _Username && u.Password == _Password)
                    select u).SingleOrDefault();
        }

        public bool CheckUserNameAvailabilty(string Username)
        {
            var x = (from u in Context.Users
                     where (u.UserName == Username) && (u.Status == (int)UserStatusEnum.Available)
                     select u).SingleOrDefault();
            if (x == null)
                return true;
            else
                return false;


        }
        public List<User> GetAvaialableWorkers()
        {
            return (from c in Context.Users
                    where (c.RoleID == 3) && (c.Status == (int)UserStatusEnum.Available) && c.CurrentStock == null
                    select c).ToList();
        }

        public User GetStockAdmin(int StockID)
        {
            return (from c in Context.Users
                    where (c.CurrentStock == StockID) && c.RoleID == 2
                    select c).SingleOrDefault();

        }
    }
}